﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace Model
{
	public partial class RoundBeam : System.Windows.Forms.Form
	{
		public RoundBeam(string title, double refWidth)
		{
			this.InitializeComponent();
			this.Text = title;
			this.refWidth = refWidth;
		}

		private void RoundBeam_Load(object sender, EventArgs e)
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			base.Icon = Resource.YArchitechIcon;
			this.LoadDlgData();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.txtOffSet.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "偏移量不能为空。");
				this.txtOffSet.Focus();
				return;
			}
			if (this.txtSpace.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "跨距不能为空。");
				this.txtSpace.Focus();
				return;
			}
			if (this.rdoAny.Checked && this.hytxtAngle.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "偏移角度不能为空。");
				this.hytxtAngle.Focus();
				return;
			}
			if (AssistFunc.mmToFeet(Convert.ToDouble(this.txtSpace.Text)) < this.refWidth)
			{
				TaskDialog.Show("盈建科提示", "跨距不应小于被跨图元的水平宽度。");
				this.txtSpace.Focus();
				return;
			}
			base.DialogResult = DialogResult.OK;
		}

        public void IsDouble(KeyPressEventArgs e, System.Windows.Forms.Control ctl)
		{
            System.Windows.Forms.TextBox textBox = null;
            if (ctl is System.Windows.Forms.TextBox)
			{
                textBox = (ctl as System.Windows.Forms.TextBox);
			}
			if (textBox != null)
			{
				if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '-' && e.KeyChar != '.')
				{
					e.Handled = true;
				}
				if (e.KeyChar == '-' && (textBox.SelectionStart != 0 || textBox.Text.IndexOf("-") >= 0))
				{
					e.Handled = true;
				}
				if (e.KeyChar == '.' && textBox.Text.IndexOf(".") >= 0)
				{
					e.Handled = true;
				}
			}
		}

		public void IsPlusDouble(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void rdoAny_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoAny.Checked)
			{
				this.hytxtAngle.Enabled = true;
				return;
			}
			this.hytxtAngle.Enabled = false;
		}

		private void txtOffSet_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsDouble(e, this.txtOffSet);
		}

		private void txtSpace_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.txtSpace.Text.Trim());
		}

		private void hytxtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.hytxtAngle.Text.Trim());
		}

		private void RoundBeam_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayConduitBaseRoundBeam"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayConduitBaseRoundBeam", "Params"));
			this.xmlHelper.SetAttribute("OffSet", this.txtOffSet.Text.Trim());
			this.xmlHelper.SetAttribute("Space", this.txtSpace.Text.Trim());
			string value = "60";
			if (this.rdo30.Checked)
			{
				value = "30";
			}
			else if (this.rdo45.Checked)
			{
				value = "45";
			}
			else if (this.rdo60.Checked)
			{
				value = "60";
			}
			else if (this.rdo90.Checked)
			{
				value = "90";
			}
			else if (this.rdoAny.Checked)
			{
				value = this.hytxtAngle.Text.Trim();
			}
			this.xmlHelper.SetAttribute("Angle", value);
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCableTrayConduitBaseRoundBeam", "Params"));
			string attribute = this.xmlHelper.GetAttribute("OffSet");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.txtOffSet.Text = attribute;
			}
			string attribute2 = this.xmlHelper.GetAttribute("Space");
			if (!string.IsNullOrEmpty(attribute2))
			{
				this.txtSpace.Text = attribute2;
			}
			string attribute3 = this.xmlHelper.GetAttribute("Angle");
			if (!string.IsNullOrEmpty(attribute3))
			{
				if (!(attribute3 == "30"))
				{
					if (!(attribute3 == "45"))
					{
						if (!(attribute3 == "60"))
						{
							if (!(attribute3 == "90"))
							{
								this.rdoAny.Checked = true;
							}
							else
							{
								this.rdo90.Checked = true;
							}
						}
						else
						{
							this.rdo60.Checked = true;
						}
					}
					else
					{
						this.rdo45.Checked = true;
					}
				}
				else
				{
					this.rdo30.Checked = true;
				}
				this.hytxtAngle.Text = attribute3;
			}
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CableTrayConduitBaseRoundBeam.xml");

		private const string Root = "Root";

		private const string Node = "HYCableTrayConduitBaseRoundBeam";

		private const string ChildNode = "Params";

		private const string OffSet = "OffSet";

		private const string Space = "Space";

		private const string Angle = "Angle";

		private double refWidth;
	}
}
